package com.ht.springcloud.order.controller;

import com.ht.springcloud.order.feign.ItemFeignClient;
import com.ht.springcloud.order.pojo.ItemOrder;
import com.ht.springcloud.order.pojo.Order;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Random;

@RestController
@RequestMapping("/orders")
public class OrderController {
   private ItemFeignClient itemFeignClient;
    @Autowired
    public void setItemFeignClient(ItemFeignClient itemFeignClient) {
        this.itemFeignClient = itemFeignClient;
    }

    @GetMapping("/order/{id}")
    @HystrixCommand(fallbackMethod = "pa",commandProperties = {@HystrixProperty(name = "execution.isolation.strategy",value = "SEMAPHORE")})
    public Order getOrderById(@PathVariable Integer id) {
        System.err.println("order收到查询请求了" + Thread.currentThread().getName());
        Order order = new Order();
        order.setId(id);
        order.setAddress("天通苑");
        ItemOrder itemById = itemFeignClient.getItemById(id + 22);
        order.setItemOrder(itemById);
        if (new Random().nextBoolean()) {
            int i = 1 / 0;
        }
        return order;

    }

    public Order pa(@PathVariable Integer id) {
        System.err.println("order出现了异常执行了降级" + Thread.currentThread().getName());
        Order order = new Order();
        order.setId(-id);
        order.setAddress("ssssssss");
        ItemOrder itemById = itemFeignClient.getItemById(id + 22);
        order.setItemOrder(itemById);
        return order;
    }
}
